<?php
/**
 * 收藏关注API逻辑层
 * @author 王勇程 <laughskydragon@qq.com>
 */

namespace app\api\logic;

use app\common\logic\Favorite as CommonFavorite;

class Favorite extends ApiBase
{
    
    public static $commonFavoriteLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonFavoriteLogic) && static::$commonFavoriteLogic = new CommonFavorite();
    }
    
    /**
     * 关注商品
     */
    public function followProduct($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用Favorite逻辑层
        $result = static::$commonFavoriteLogic->followProduct($data);
        
        // API逻辑层直接返回common层的结果
        return $result;
    }

    /**
     * 取消关注商品
     */
    public function unfollowProduct($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用Favorite逻辑层
        $result = static::$commonFavoriteLogic->unfollowProduct($data);
        
        // API逻辑层直接返回common层的结果
        return $result;
    }

    /**
     * 检查商品关注状态
     */
    public function checkFollowStatus($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用Favorite逻辑层
        $result = static::$commonFavoriteLogic->checkFollowStatus($data);
        
        // API逻辑层直接返回common层的结果
        return $result;
    }

    /**
     * 获取关注列表
     */
    public function getFollowList($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用Favorite逻辑层
        $result = static::$commonFavoriteLogic->getFollowList($data);
        
        // API逻辑层直接返回common层的结果
        return $result;
    }

    /**
     * 切换收藏状态
     */
    public function toggleFavorite($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 检查当前关注状态
        $checkResult = static::$commonFavoriteLogic->checkFollowStatus($data);
        
        if ($checkResult) {
            // 已关注，执行取消关注
            $result = static::$commonFavoriteLogic->unfollowProduct($data);
        } else {
            // 未关注，执行关注
            $result = static::$commonFavoriteLogic->followProduct($data);
        }
        
        return $result[0] == RESULT_SUCCESS ? [API_CODE_NAME=>0, API_MSG_NAME=>'操作成功'] : [API_CODE_NAME=>-1, API_MSG_NAME=>'操作失败'];
    }

    /**
     * 设置价格提醒
     */
    public function setPriceAlert($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用PriceAlert逻辑层
        $priceAlertLogic = new \app\common\logic\PriceAlert();
        $result = $priceAlertLogic->setPriceAlert($data);
        
        return $result;
    }

    /**
     * 获取价格提醒列表
     */
    public function getPriceAlertList($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用PriceAlert逻辑层
        $priceAlertLogic = new \app\common\logic\PriceAlert();
        $result = $priceAlertLogic->getPriceAlertList($data);
        
        return $result;
    }

    /**
     * 删除价格提醒
     */
    public function deletePriceAlert($data = [])
    {
        // JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }
        
        // 调用PriceAlert逻辑层
        $priceAlertLogic = new \app\common\logic\PriceAlert();
        $result = $priceAlertLogic->deletePriceAlert($data);
        
        return $result;
    }
}